package com.ww.springboot.springstudy.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

/**
 * 描述：springboot 提供的全局请求拦截
 *
 * @author 🧑 ‍wanwei
 * @since 2024-01-31 15:00
 */
@Component
public class FilterRegistrationBeanConfig {

	@Bean
	public FilterRegistrationBean springMvcMDCFilterProxyMy() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		FilterMy filter = new FilterMy();
		filterRegistrationBean.setFilter(filter);
		filterRegistrationBean.setName(filter.getFilterName());
		filterRegistrationBean.setAsyncSupported(true);
		filterRegistrationBean.setOrder(1);
		return filterRegistrationBean;
	}

    public class FilterMy implements Filter {
        public FilterMy() {
        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println(servletRequest);
            System.out.println(servletResponse);
            System.out.println(filterChain);
            filterChain.doFilter(servletRequest, servletResponse);
        }

        /**
         * 名称不能重复
         * @return
         */
        public String getFilterName() {
            return "FilterMy";
        }
    }


}
